package com.ww.utils;

import java.io.UnsupportedEncodingException;
import java.net.URISyntaxException;
import java.util.HashMap;
import java.util.Map;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.ww.App;

public class BaiduLbsUtils {
	
	static Logger logger = LoggerFactory.getLogger(App.class);
	
	private static final String AK = "a49QpA1I2HVWCXL3ULMLvPxlU9Chpt98";
	private static final String GEOTABLE_ID = "148148";
	
	private static int PAGE_INDEX = 0;
	private static final int PAGE_SIZE = 200;
	
	private static final String API_POI_LIST = "http://api.map.baidu.com/geodata/v3/poi/list";
	private static final String API_POI_CREATE = "http://api.map.baidu.com/geodata/v3/poi/create";
	private static final String API_POI_UPDATE = "http://api.map.baidu.com/geodata/v3/poi/update";
	private static final String API_POI_DEL = "http://api.map.baidu.com/geodata/v3/poi/delete";
	
	
	public static void listPois(String title) throws URISyntaxException{
		int currentSize = 0;
		
		Map<String, Object> params = new HashMap<>();
		params.put("ak", AK);
		params.put("geotable_id", GEOTABLE_ID);
		params.put("page_index", PAGE_INDEX);
		params.put("page_size", PAGE_SIZE);
		params.put("title", title);
		String rs = HttpClientUtils.httpGetRequest(API_POI_LIST, params);
		
		JSONObject json = JSONObject.parseObject(rs);
		int size = json.getIntValue("size");
		int total = json.getIntValue("total");
		currentSize += size;
		
		JSONArray pois = json.getJSONArray("pois");
		if(pois != null && pois.size() > 0){
			StringBuffer sb = new StringBuffer();
			for (int i = 0; i < pois.size(); i++) {
				sb.append(pois.getJSONObject(i).toJSONString() + "\n");
			}
			logger.info("poi size > " + pois.size());
			logger.info(sb.toString());
		}
		if(currentSize < total){
			PAGE_INDEX += 1;
			listPois(title);
		}
		
	}
	
	public static void createPoi(String title, String address, String tags, double latitude, Double longitude, int coord_type, String pushid, String schoolid) throws UnsupportedEncodingException{
		Map<String, Object> params = new HashMap<>();
		params.put("ak", AK);
		params.put("geotable_id", GEOTABLE_ID);
		
		params.put("title", title);
		params.put("address", address);
		params.put("tags", tags);
		params.put("latitude", latitude);
		params.put("longitude", longitude);
		params.put("coord_type", coord_type);
		params.put("pushid", pushid);
		params.put("schoolid", schoolid);
		
		String rs = HttpClientUtils.httpPostRequest(API_POI_CREATE, params);
		logger.info(StringUtils.ascii2native(rs));
	}
	
	
	public static void updatePoi(String id, String title, String address, double latitude, Double longitude, int coord_type) throws UnsupportedEncodingException{
		Map<String, Object> params = new HashMap<>();
		params.put("ak", AK);
		params.put("geotable_id", GEOTABLE_ID);
		params.put("id", id);
		params.put("title", title);
		params.put("address", address);
		params.put("latitude", latitude);
		params.put("longitude", longitude);
		params.put("coord_type", coord_type);
		
		String rs = HttpClientUtils.httpPostRequest(API_POI_UPDATE, params);
		logger.info(StringUtils.ascii2native(rs));
	}

	public static void delPoi(String id, int coord_type) throws UnsupportedEncodingException{
		Map<String, Object> params = new HashMap<>();
		params.put("ak", AK);
		params.put("geotable_id", GEOTABLE_ID);
		params.put("id", id);
		params.put("coord_type", coord_type);
		
		String rs = HttpClientUtils.httpPostRequest(API_POI_DEL, params);
		logger.info(StringUtils.ascii2native(rs));
	}
	
	public static void main(String[] args) throws URISyntaxException, UnsupportedEncodingException {
//		listPois("武大明涛驾校");
		createPoi("兰基驾校鲇鱼套分校", "武汉市武昌区鲇鱼套公交车站场,武汉", "school", 30.5317380321, 114.2983719056, 3, "", "15f627de04424e59ac66660a166beb4b");
//		updatePoi("1924722178", "绅宝驾校", "武汉市青山区和平公园,武汉", 30.6396278860, 114.38646325776, 3);
	}
	
}
